package com.geez.bbl;

import com.geez.bbl.db.type.BblBook;
import com.geez.bbl.db.type.BblChapter;
import com.geez.bbl.db.type.Builder;
import com.geez.bbl.db.type.Verse;
import com.geez.bbl.util.FileUtils;
import com.geez.bbl.util.GeezUtil;
import com.geez.bbl.util.ProjectResources;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: biniam
 * Date: 4/7/12
 * Time: 8:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class ChildName {


    private static File childNameFile = ProjectResources.getResourceFile("childNames.csv");

    private static File childNameFile2 = ProjectResources.getResourceFile("childNames2.csv");

    public static void main(String[] args) throws Exception {
        int i = 0;
      Map<String, String> names = FileUtils.processCSV(childNameFile);
       StringBuilder b = new StringBuilder();

        List<BblBook> enBook = new Builder("en").build();
       //List<BblBook> tiBook = new Builder("ti").build();

        for (Map.Entry<String, String> entry : names.entrySet()) {
                    System.out.println((i++) + ". entry.getKey() = " + entry.getKey());
            System.out.println("entry.getValue() = " + entry.getValue());
            String d = "";
            for (BblBook bblBook : enBook) {
                for (Map.Entry<Integer, BblChapter> bblChapterEntry : bblBook.getChapters().entrySet()) {
                    for (Verse verse : bblChapterEntry.getValue().getVerses()) {
                        if(verse.getPlainValue().contains(entry.getKey())){
                          d= d + ",["+bblBook.getBook_no()+","+bblChapterEntry.getKey()+","+verse.getStartId()+","+verse.getEndId()+"]";

                        //

 //  System.out.println("verse = " + tiV(tiBook,entry.getKey(), bblBook.getBook_no(),bblChapterEntry.getKey(),verse.getStartId(),verse.getEndId()));
                         //   System.out.println(bblBook.getEnglishName()+ " "+ bblChapterEntry.getKey()+" "+ verse);
                    }
                }
            }

        }
        b.append(entry.getKey()).append(",")
                .append("{")
                .append(d)
                .append("},")
                .append(entry.getValue()).append("\n");
    }

        FileUtils.save(childNameFile2,b.toString());
    }

    private static  String tiV(List<BblBook> tiBook, String name, int b,int c, int start, int end){
        BblBook book = tiBook.get(b-1);
        BblChapter chapter= book.getChapter(c);
        Verse v = chapter.getVerse(start, end);

        if(v==null){
             return null;
        }

        String key = GeezUtil.removeVowel(name).toLowerCase();
        String r = "";
        for (String s : v.getValue().split("\\s+")) {
            String s2 = GeezUtil.removeVowel(GeezUtil.toSera(s)).toLowerCase();
            if(s2.equals(key)){
                r = r + " "+s;
            }

        }
        return book.getShort_name()+ " "+ chapter.getId()+" "+ r;
    }
}
